import { loginAPI } from '@/api/Login/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: () => {
    return {
      token: getToken() || ''
    }
  },
  mutations: {
    // 保存token
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    // 清除token
    clearToken(state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    // 调用接口
    async login(context, { username, password }) {
      const res = await loginAPI({ username, password })
      // 保存vuex
      context.commit('setToken', res.data.token)
    }
  },
  getters: {}
}
